
#include <asm.h>

//.global C_SYMBOL(AlphaBlit888WMMX)


C_SYMBOL(AlphaBlit888WMMX):

nHeight =-0x2C
nWidth =-0x28
nDstPitch =-0x24
nSrcPitch =-0x20
pDB = -0x1C
pDG = -0x18
pDR= -0x14
pSB= -0x10
pSG= -0xC
pSR= -8
pAlpha= -4

    STMFD	SP!, {R4-R12,LR}
    SUB     SP, SP, #0x30
@.long 0xe7fddefe

    LDR     R6, [R0,#0x0]
    STR     R6, [SP,#0x30+pAlpha]

    LDR     R7, [R0,#0x4]
    STR     R7, [SP,#0x30+pSR]

    LDR     R8, [R0,#0x8]
    STR     R8, [SP,#0x30+pSG]

    LDR     R9, [R0,#0xC]
    STR     R9, [SP,#0x30+pSB]

    LDR     R10, [R0,#0x10]
    STR     R10, [SP,#0x30+pDR]

    LDR     R11, [R0,#0x14]
    STR     R11, [SP,#0x30+pDG]

    LDR     R12, [R0,#0x18]
    STR     R12, [SP,#0x30+pDB]

    LDR     R4, [R0,#0x1C]
    STR     R4, [SP,#0x30+nSrcPitch]

    LDR     R4, [R0,#0x20]
    STR     R4, [SP,#0x30+nDstPitch]

    LDR     R5, [R0,#0x24]
    STR     R5, [SP,#0x30+nWidth]

    LDR     R4, [R0,#0x28]
    STR     R4, [SP,#0x30+nHeight]


    LDR     R2, = 0x00FF00FF
    TMCRR   wR15 , R2, R2

    LDR     R2, = 0x00000008
    TMCR    wCGR0, R2


    TMCRR    wR0, R2 , R2

    LDR	R0, =0xFFFFFFF8
    LDR	R1, =0xFFFFFFFF
    LDR	LR, =0xFF

PrimeLoop:

    WLDRD       wR8, [R6]       @A
    WLDRD       wR9, [R7]       @SR
    WLDRD       wR10, [R8]      @SG
    WLDRD       wR11, [R9]      @SB
    WLDRD       wR12, [R10]     @DR
    WLDRD       wR13, [R11]     @DG
    WLDRD       wR14, [R12]     @DB


LoopQWord:


@    LDR     R3, [R6]
@   TST     R5, R0                                              @check if only 7 pixels left
@   BEQ     CheckBack

   TST R5, LR
   BEQ  NextLine

@early out tests
@.long 0xe7fddefe
@    CMP     R3, R1
@    BEQ     CopyBack

@    TST     R3, R1
@    BEQ     LeaveFront

@the alpha blend starts

    WUNPCKELUB  wR7, wR8
    WUNPCKELUB  wR1, wR9
    WMULUL  wR1,wR1,wR7
    WUNPCKELUB  wR2, wR10
    WMULUL  wR2,wR2,wR7
    WUNPCKELUB  wR3, wR11
    WMULUL  wR3,wR3,wR7
    WSUBH   wR7,wR15,wR7

    WUNPCKELUB  wR4, wR12
    WMULUL  wR4,wR4,wR7
    WUNPCKELUB  wR5, wR13
    WMULUL  wR5,wR5,wR7
    WUNPCKELUB  wR6, wR14
    WMULUL  wR6,wR6,wR7

    WADDH    wR1,wR1,wR4
    WADDH    wR2,wR2,wR5
    WADDH    wR3,wR3,wR6

    WUNPCKEHUB  wR7, wR8
    WLDRD       wR8, [R6, #8]!       @A
    WUNPCKEHUB  wR4, wR12
    WLDRD       wR12, [R10, #8]     @DR
    WUNPCKEHUB  wR5, wR13
    WLDRD       wR13, [R11, #8]     @DG
    WUNPCKEHUB  wR6, wR14
    WLDRD       wR14, [R12, #8]     @DB

    WSRLHG  wR1, wR1, wCGR0
    WSRLHG  wR2, wR2, wCGR0
    WSRLHG  wR3, wR3, wCGR0

    WPACKHUS  wR1, wR1, wR0
    WPACKHUS  wR2, wR2, wR0
    WPACKHUS  wR3, wR3, wR0

    WSTRW   wR1, [R10]
    WSTRW   wR2, [R11]
    WSTRW   wR3, [R12]

    WUNPCKEHUB  wR1, wR9
    WMULUL  wR1,wR1,wR7
    WLDRD       wR9, [R7, #8]!       @SR
    WUNPCKEHUB  wR2, wR10
    WMULUL  wR2,wR2,wR7
    WLDRD       wR10, [R8, #8]!      @SG
    WUNPCKEHUB  wR3, wR11
    WMULUL  wR3,wR3,wR7
    WLDRD       wR11, [R9, #8]!      @SB

    WSUBH   wR7,wR15,wR7

    WMULUL  wR4,wR4,wR7
    ADD     R10, R10, #8
    WMULUL  wR5,wR5,wR7
    ADD     R11, R11, #8
    WMULUL  wR6,wR6,wR7
    ADD     R12, R12, #8


    WADDH    wR1,wR1,wR4
    WADDH    wR2,wR2,wR5
    WADDH    wR3,wR3,wR6

    WSRLHG  wR1, wR1, wCGR0
    WSRLHG  wR2, wR2, wCGR0
    WSRLHG  wR3, wR3, wCGR0

    WPACKHUS  wR1, wR1, wR0
    WPACKHUS  wR2, wR2, wR0
    WPACKHUS  wR3, wR3, wR0

    WSTRW   wR1, [R10, #-4]
    WSTRW   wR2, [R11, #-4]
    WSTRW   wR3, [R12, #-4]

    SUB     R5, R5, #8

    B LoopQWord

CopyBack:
@.long 0xe7fddefe

    LDR     R3,[R6, #4]
    WSTRD   wR9, [R10]
    WSTRD   wR10, [R11]
    WSTRD   wR11, [R12]

    ADD     R6, R6, #8
    ADD     R7, R7, #8
    ADD     R8, R8, #8
    ADD     R9, R9, #8
    ADD     R10, R10, #8
    ADD     R11, R11, #8
    ADD     R12, R12, #8

    SUB     R5, R5, #8

    CMP     R3, R1
    BEQ     PrimeLoop

    WUNPCKEHUB  wR7, wR8
    WUNPCKEHUB  wR1, wR9
    WMULUL  wR1,wR1,wR7
    WUNPCKEHUB  wR2, wR10
    WMULUL  wR2,wR2,wR7
    WUNPCKEHUB  wR3, wR11
    WMULUL  wR3,wR3,wR7
    WSUBH   wR7,wR15,wR7

    WUNPCKEHUB  wR4, wR12
    WMULUL  wR4,wR4,wR7
    WUNPCKEHUB  wR5, wR13
    WMULUL  wR5,wR5,wR7
    WUNPCKEHUB  wR6, wR14
    WMULUL  wR6,wR6,wR7

    WADDH    wR1,wR1,wR4
    WADDH    wR2,wR2,wR5
    WADDH    wR3,wR3,wR6

    WSRLHG  wR1, wR1, wCGR0
    WSRLHG  wR2, wR2, wCGR0
    WSRLHG  wR3, wR3, wCGR0

    WPACKHUS  wR1, wR1, wR0
    WPACKHUS  wR2, wR2, wR0
    WPACKHUS  wR3, wR3, wR0

    WSTRW   wR1, [R10, #-4]
    WSTRW   wR2, [R11, #-4]
    WSTRW   wR3, [R12, #-4]

    B PrimeLoop

LeaveFront:

    LDR     R3,[R6, #4]
    ADD     R6, R6, #8
    ADD     R7, R7, #8
    ADD     R8, R8, #8
    ADD     R9, R9, #8
    ADD     R10, R10, #8
    ADD     R11, R11, #8
    ADD     R12, R12, #8

    SUB     R5, R5, #8

    TST     R3, R1
    BEQ     PrimeLoop

    WUNPCKEHUB  wR7, wR8
    WUNPCKEHUB  wR1, wR9
    WMULUL  wR1,wR1,wR7
    WUNPCKEHUB  wR2, wR10
    WMULUL  wR2,wR2,wR7
    WUNPCKEHUB  wR3, wR11
    WMULUL  wR3,wR3,wR7
    WSUBH   wR7,wR15,wR7

    WUNPCKEHUB  wR4, wR12
    WMULUL  wR4,wR4,wR7
    WUNPCKEHUB  wR5, wR13
    WMULUL  wR5,wR5,wR7
    WUNPCKEHUB  wR6, wR14
    WMULUL  wR6,wR6,wR7

    WADDH    wR1,wR1,wR4
    WADDH    wR2,wR2,wR5
    WADDH    wR3,wR3,wR6

    WSRLHG  wR1, wR1, wCGR0
    WSRLHG  wR2, wR2, wCGR0
    WSRLHG  wR3, wR3, wCGR0

    WPACKHUS  wR1, wR1, wR0
    WPACKHUS  wR2, wR2, wR0
    WPACKHUS  wR3, wR3, wR0

    WSTRW   wR1, [R10, #-4]
    WSTRW   wR2, [R11, #-4]
    WSTRW   wR3, [R12, #-4]


    B PrimeLoop

CheckBack:
@.long 0xe7fddefe
   TST R5, LR
   BEQ  NextLine

@backalign: work out back end pixels
@.long 0xe7fddefe


NextLine:
@.long 0xe7fddefe
    SUBS     R4, R4, #1
    BEQ     Done

    LDR     R6, [SP,#0x30+pAlpha]
    LDR     R7, [SP,#0x30+pSR]
    LDR     R8, [SP,#0x30+pSG]
    LDR     R9, [SP,#0x30+pSB]
    LDR     R10, [SP,#0x30+pDR]
    LDR     R11, [SP,#0x30+pDG]
    LDR     R12, [SP,#0x30+pDB]
    LDR     R2,  [SP,#0x30+nSrcPitch]
    LDR     R3,  [SP,#0x30+nDstPitch]
    ADD     R6, R6, R2
    ADD     R7, R7, R2
    ADD     R8, R8, R2
    ADD     R9, R9, R2
    ADD     R10, R10, R3
    ADD     R11, R11, R3
    ADD     R12, R12, R3
    STR     R6, [SP,#0x30+pAlpha]
    STR     R7, [SP,#0x30+pSR]
    STR     R8, [SP,#0x30+pSG]
    STR     R9, [SP,#0x30+pSB]
    STR     R10, [SP,#0x30+pDR]
    STR     R11, [SP,#0x30+pDG]
    STR     R12, [SP,#0x30+pDB]

    LDR     R5, [SP,#0x30+nWidth]
    B       PrimeLoop

Done:

ADD     SP, SP, #0x30
LDMFD	SP!, {R4-R12,PC}
